package com.hp.ecs.xqa.util.orm;

import java.util.Collections;
import java.util.List;

/**
 * @author fanhoujun 2007-9-10
 */
public class PageList<T> {

	public static final int DEFAULT_PAGE_SIZE = 20;

	private final int page;

	private final int pageSize;

	private int totalCount;

	private List<T> list = Collections.EMPTY_LIST;

	private String[] columnNames;

	public static final PageList EMPTY = new PageList(0, DEFAULT_PAGE_SIZE);

	/**
	 * @param page begin with 0
	 * @param pageSize
	 */
	public PageList(int page, int pageSize) {
		this.page = page;
		this.pageSize = pageSize;
	}

	public boolean isDefaultEmpty() {
		return EMPTY == this;
	}

	public int firstIndex() {
		return pageSize * page;
	}

	public int getPage() {
		return page;
	}

	public int pageSize() {
		return pageSize;
	}

	public int getPageSize() {
		return pageSize;
	}

	public int size() {
		return list.size();
	}

	public void setResult(List<T> list) {
		this.list = list;
	}

	public int getTotalCount() {
		return totalCount;
	}

	public void setTotalCount(int totalCount) {
		this.totalCount = totalCount;
	}

	public int pageCount() {
		return totalCount / pageSize + (totalCount % pageSize == 0 ? 0 : 1);
	}

	public int getPageCount() {
		return pageCount();
	}

	public List<T> getList() {
		return list;
	}

	public boolean hasNext() {
		return getPage() < pageCount() - 1;
	}

	public String[] getColumnNames() {
		return columnNames;
	}

	public void setColumnNames(String[] columnNames) {
		this.columnNames = columnNames;
	}

}
